//
//  ViewFactory.m
//  DeyiLife
//
//  Created by zxin on 15/5/27.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "ViewFactory.h"

@interface ViewFactory ()

@property (strong, nonatomic) id LoginParent;

@end

@implementation ViewFactory

DEFINE_SINGLETON_IMPLEMENTATION(ViewFactory)

- (UIScrollView *)makePageScrollView:(NSInteger)pageNums topBarHeight:(CGFloat)height {
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1000)];
    [scrollview setShowsHorizontalScrollIndicator:NO];
    [scrollview setShowsVerticalScrollIndicator:NO];
    scrollview.contentSize = CGSizeMake(ScreenWidth * pageNums, ScreenHeight - TopHeight - TabBarHeight);
    [scrollview setPagingEnabled:YES];
    return scrollview;
}

- (UITableView *)makeTableView:(id)delegate Height:(CGFloat)height{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, height) style:UITableViewStylePlain];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
  //  [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.showsVerticalScrollIndicator = NO;
    return tableView;
}


- (UITableView *)makeTableView:(id)delegate Height:(CGFloat)height  Width:(CGFloat)width
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
    tableView.tableHeaderView = [UIView new];
    tableView.tableFooterView = [UIView new];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.showsVerticalScrollIndicator = NO;
    return tableView;
}


- (UIScrollView *)makePageScrollViewWithFrame:(CGRect)frameRect andPageNums:(NSInteger)pageNums topBarHeight:(CGFloat)height
{
    UIScrollView *scrollerView = [[UIScrollView alloc]initWithFrame:frameRect];
    [scrollerView setShowsHorizontalScrollIndicator:NO];
    [scrollerView setShowsVerticalScrollIndicator:NO];
    scrollerView.contentSize = CGSizeMake(frameRect.size.width*pageNums, ScreenHeight-TopHeight-TopTabBarHeight);
    [scrollerView setPagingEnabled:YES];
    return scrollerView;
}


- (UIScrollView *)resetPageScrollViewWithScrollView:(UIScrollView *)scrollView andFrame:(CGRect)frameRect andPageNums:(NSInteger)pageNums topBarHeight:(CGFloat)height
{
    [scrollView setFrame:frameRect];
    [scrollView setContentSize:CGSizeMake(frameRect.size.width*pageNums, ScreenHeight-TopHeight-TopTabBarHeight)];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView setPagingEnabled:YES];
    return scrollView;

}

+ (UITableViewCell *)makeSearchKeyCell:(UITableView *)tableView {
    UITableViewCell *cell = GET_SYSTEM_CELL(Default, @"SearchKeyCell")
    cell.textLabel.font = [FontTool fontWithSize:15];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"3a3a3a"];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(40, 42, ScreenWidth - 40 - 20, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    [cell.contentView addSubview:line];
    return cell;
}

#pragma mark - navigation
+ (void)setNavigationBlack:(UIViewController *)vc {
    if(!ISIOS6) {
         [vc.navigationController.navigationBar setBarTintColor:NavigationColor];
    }
   
    vc.navigationController.navigationBar.translucent = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

+ (void)setNavigationWhite:(UIViewController *)vc {
    if(!ISIOS6)
    {
        [vc.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    }
    
    vc.navigationController.navigationBar.translucent = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

+ (UIBarButtonItem *)makeNavigationBack:(id)target action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0.0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"backbtn"] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    return backbtn;
}

+ (UIBarButtonItem *)makeNavigationBackWhite:(id)target action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0.0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"backbtnw"] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    return backbtn;
}

+ (UIBarButtonItem *)makeNavigationBack:(id)target action:(SEL)action backImg:(UIImage *)image {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, 30, 30);
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    return backbtn;
}

+ (UILabel *)makeNavigationTitle:(NSString *)title {
    UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 50, 0, 100, 50)];
    titleText.backgroundColor = [UIColor clearColor];
    titleText.textColor = [UIColor whiteColor];
    //titleText.textColor = [UIColor colorWithHexString:@"#151515"];
    titleText.textAlignment = NSTextAlignmentCenter;
    [titleText setFont:[UIFont systemFontOfSize:16]];
    [titleText setText:title];
    return titleText;
}

+ (UILabel *)makeNavigationTitle:(NSString *)title  WithTextColor:(UIColor *)textColor {
    UILabel *titleText = [self makeNavigationTitle:title];
    titleText.textAlignment = NSTextAlignmentCenter;
    if(!textColor)
    {
        textColor = [UIColor colorWithHexString:@"#151515"];
    }
    titleText.textColor = textColor;
    return titleText;
}

+ (UILabel *)makeNavigationTitleBlack:(NSString *)title {
    UILabel *titleText = [self makeNavigationTitle:title];
    titleText.textColor = [UIColor colorWithHexString:@"#151515"];
    return titleText;
}

+ (UIButton *)makeNavigationTextButton:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 44);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitleColor:[UIColor colorWithHexString:@"#151515"] forState:UIControlStateNormal];
    return button;
}

+ (UIButton *)makeNavigationTextButtonWhite:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 60, 44);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return button;
}

+ (UIButton *)makeNavigationBtnWithText:(NSString *)title  andImage:(NSString *)imageName andTextCol:(UIColor *)color {
    UIButton *button = [self makeNavigationTextButton:title];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    return button;
}

+ (UIButton *)makeNavigationBtnWithText:(NSString *)title  andImage:(NSString *)imageName {
    UIButton *button = [self makeNavigationTextButton:title];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    return button;
}

+ (UIImage *)makeShareView:(UIImage *)image text:(NSString *)text {
    UIView *shareview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 312 + 70 + 16 + 40 + 20)];
    UIView *bgview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 312 + 70 + 16 + 40 + 20)];
    bgview.backgroundColor = [UIColor whiteColor];
    [shareview addSubview:bgview];
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 300) / 2, 12, 300, 300)];
    [shareview addSubview:imageview];
    imageview.image = image;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [paragraphStyle setLineSpacing:8];
    //NSInteger len = text.length;
    if (text.length <= 12) {
        UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 300) / 2, 312 + 10, 300, 60)];
        titleText.numberOfLines = 0;
        titleText.textColor = [UIColor colorWithHexString:@"#3d3d3d"];
        titleText.textAlignment = NSTextAlignmentCenter;
        [titleText setFont:[UIFont systemFontOfSize:18]];
        titleText.attributedText = [[NSAttributedString alloc]initWithString:text attributes: @{ NSFontAttributeName:[UIFont systemFontOfSize:18],NSParagraphStyleAttributeName:paragraphStyle}];
        [shareview addSubview:titleText];
    } else if (text.length > 12 && text.length <= 25) {
        NSString *text1 = [text substringToIndex:12];
        NSString *text2 = [text substringFromIndex:12];
        if (text.length == 25) {
            text1 = [text substringToIndex:13];
            text2 = [text substringFromIndex:13];
        }
        UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 300) / 2, 312 + 10, 300, 30)];
        titleText.numberOfLines = 0;
        titleText.textColor = [UIColor colorWithHexString:@"#3d3d3d"];
        titleText.textAlignment = NSTextAlignmentCenter;
        [titleText setFont:[UIFont systemFontOfSize:15]];
        titleText.attributedText = [[NSAttributedString alloc]initWithString:text1 attributes: @{ NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle}];
        [shareview addSubview:titleText];
        UILabel *titleText2 = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 300) / 2, 312 + 40, 300, 30)];
        titleText2.numberOfLines = 0;
        titleText2.textColor = [UIColor colorWithHexString:@"#3d3d3d"];
        titleText2.textAlignment = NSTextAlignmentCenter;
        [titleText2 setFont:[UIFont systemFontOfSize:15]];
        titleText2.attributedText = [[NSAttributedString alloc]initWithString:text2 attributes: @{ NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle}];
        [shareview addSubview:titleText2];
    } else {
        NSString *text1 = [text substringToIndex:25];
        NSString *text2 = [text substringFromIndex:25];
        UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 320) / 2, 312 + 10, 320, 30)];
        titleText.numberOfLines = 0;
        titleText.textColor = [UIColor colorWithHexString:@"#3d3d3d"];
        titleText.textAlignment = NSTextAlignmentCenter;
        [titleText setFont:[UIFont systemFontOfSize:12]];
        titleText.attributedText = [[NSAttributedString alloc]initWithString:text1 attributes: @{ NSFontAttributeName:[UIFont systemFontOfSize:12],NSParagraphStyleAttributeName:paragraphStyle}];
        [shareview addSubview:titleText];
        UILabel *titleText2 = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 320) / 2, 312 + 40, 320, 30)];
        titleText2.numberOfLines = 0;
        titleText2.textColor = [UIColor colorWithHexString:@"#3d3d3d"];
        titleText2.textAlignment = NSTextAlignmentCenter;
        [titleText2 setFont:[UIFont systemFontOfSize:12]];
        titleText2.attributedText = [[NSAttributedString alloc]initWithString:text2 attributes: @{ NSFontAttributeName:[UIFont systemFontOfSize:12],NSParagraphStyleAttributeName:paragraphStyle}];
        [shareview addSubview:titleText2];
    }
    
    UIImageView *qrcode = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 40) / 2, 312 + 70 + 16, 40, 40)];
    [shareview addSubview:qrcode];
    qrcode.layer.masksToBounds = YES;
    qrcode.contentMode = UIViewContentModeScaleAspectFill;
    qrcode.image = [UIImage imageNamed:@"qrcode"];
    
    UIGraphicsBeginImageContextWithOptions(shareview.bounds.size, NO, 0.0f);
    
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //渲染控制器的view的图层到上下文
    //图层只能用render（渲染） 不能用draw（画）
    [shareview.layer renderInContext:ctx];
    
    //获取截屏图片
    UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return final;
}

#pragma mark - notice
+ (void)dismissLoading {
    [SVProgressHUD dismiss];
}

+ (void)setHUDStyle {
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    if (ISIOS8) {
       [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
    }
}

+ (void)showLoadingEnableTouch {
    [self setHUDStyle];
    [SVProgressHUD showWithStatus:nil];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
}

+ (void)showLoadingEnableTouch:(NSString *)content {
    [self setHUDStyle];
    [SVProgressHUD showWithStatus:content];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
}

+ (void)showLoading {
    [self setHUDStyle];
    [SVProgressHUD showWithStatus:nil];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

+ (void)showLoading:(NSString *)content {
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:content];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

+ (void)showSuccess:(NSString *)content {
    [self setHUDStyle];
    [SVProgressHUD showSuccessWithStatus:content];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

+ (void)showInfoMsg:(NSString *)content {
    [self setHUDStyle];
    [SVProgressHUD showInfoWithStatus:content];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

+ (void)showNoMoreData {
    [self setHUDStyle];
    [SVProgressHUD showInfoWithStatus:@"没有下一页了" maskType:SVProgressHUDMaskTypeNone];
}

#pragma mark alertview delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (buttonIndex == 1) { //确定
//        if (self.LoginParent) {
//            [ViewFactory showLogin:self.LoginParent];
//        }
//    }
//    [DataCenter sharedDataCenter].accountDialogShow = NO;
}

- (void)showAccountException:(id)context {
//    if (![DataCenter sharedDataCenter].accountDialogShow) {
//        [DataCenter sharedDataCenter].accountDialogShow = YES;
//        [SVProgressHUD dismiss];
//        [UserData setLastUserName:[UserData username]];
//        [UserData setLastPassWord:[UserData password]];
//        [UserData logout];
//        self.LoginParent = context;
//        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"账号异常，请重新登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        [alertview show];
//    }
}

+ (void)showErrorMsg:(YTKBaseRequest *)request {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[request.responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
    [SVProgressHUD showErrorWithStatus:[dict objectForKey:@"msg"] maskType:SVProgressHUDMaskTypeClear];
}

+ (void)showErrorStrMsg:(NSString *)content {
    [self setHUDStyle];
    [SVProgressHUD showErrorWithStatus:content maskType:SVProgressHUDMaskTypeClear];
}

#pragma mark - show login
+ (void)showLogin:(UIViewController *)context {
//    LoginViewController *vc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
//    vc.type = ILoginEnter;
//    vc.isAccountLoginExpire = YES;
//    UINavigationController *navlogin = [[UINavigationController alloc] initWithRootViewController:vc];
//    [context presentViewController:navlogin animated:YES completion:nil];
}

//#pragma mark - cells
//+ (UITableViewCell *)makeSearchKeyCell:(UITableView *)tableView {
//    UITableViewCell *cell = GET_SYSTEM_CELL(Default, @"SearchKeyCell")
//    cell.textLabel.font = [FontTool fontWithSize:15];
//    cell.textLabel.textColor = [UIColor colorWithHexString:@"3a3a3a"];
//    
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(40, 42, ScreenWidth - 40 - 20, 1)];
//    line.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
//    [cell.contentView addSubview:line];
//    return cell;
//}
//
//+ (UITableViewCell *)makeSearchClearCell:(UITableView *)tableView {
//    UITableViewCell *cell = GET_SYSTEM_CELL(Default, @"SearchClearCell")
//    cell.contentView.bounds = CGRectMake(0, 0, ScreenWidth, 36);
//    UILabel *title = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
//    title.text = @"清空历史记录";
//    title.font = [FontTool fontWithSize:14];
//    title.textColor = [UIColor colorWithHexString:@"656565"];
//    title.textAlignment = NSTextAlignmentCenter;
//    [cell.contentView addSubview:title];
//    return cell;
//}
//
//#pragma mark headrefresh
//+ (void)addRefreshHeader:(UITableView *)tableview {
//    MJRefreshGifHeader *headerGif = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
//    headerGif.lastUpdatedTimeLabel.hidden = YES;
//    [headerGif setImages:[RefreshComponent sharedRefreshComponent].idleImages forState:MJRefreshStateIdle];
//    [headerGif setImages:[RefreshComponent sharedRefreshComponent].refreshImages forState:MJRefreshStatePulling];
//    [headerGif setImages:[RefreshComponent sharedRefreshComponent].refreshImages forState:MJRefreshStateRefreshing];
//    tableview.header = headerGif;
//}

+ (NSInteger)calculateHeight:(NSString *)content fontSize:(NSInteger)size delta:(NSInteger)delta {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:delta];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont systemFontOfSize:size];
    textLabel.attributedText = attributedString;
    //textLabel.text = test;//detail.path.content;
    textLabel.numberOfLines = 0;//根据最大行数需求来设置
    textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    CGSize maximumLabelSize = CGSizeMake((ScreenWidth - 30), 9999);//labelsize的最大值
    //关键语句
    CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
    return expectSize.height;
}

@end
